package com.coming.mySpring.system.service.impl;

import com.coming.mySpring.constant.BaseConstant;
import com.coming.mySpring.exception.BusinessException;
import com.coming.mySpring.org.domain.Employee;
import com.coming.mySpring.org.mapper.EmployeeMapper;
import com.coming.mySpring.system.domain.Shop;
import com.coming.mySpring.system.mapper.ShopMapper;
import com.coming.mySpring.system.query.ShopQuery;
import com.coming.mySpring.system.service.IShopService;
import com.coming.mySpring.base.service.impl.BaseServiceImpl;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jjy
 * @since 2023-01-07
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(Shop shop) {
        //一堆判断
        if(shop.getName() == null){
            throw new BusinessException("店铺名不能为空");
        }
        if(shop.getAddress() == null){
            throw new BusinessException("地址不能为空");
        }
        if(shop.getTel() == null){
            throw new BusinessException("联系方式不能为空");
        }

        //校验店铺是否存在
        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setName(shop.getName());
        Long count = shopMapper.queryCount(shopQuery);
        System.out.println(count);
        if(count!=0){
            throw new BusinessException(shop.getName()+"       店铺名已存在");
        }
        //补充默认数据
        shop.setRegisterTime(new Date());//注册时间
        shop.setState(BaseConstant.Shop.STATE_WAIT_AUTID);//审核状态

//        shop.setAdminId(?);
        //获取到当前注册的管理员信息
        Employee admin = shop.getAdmin();
        //保存数据库 才能得到 admin_id
        employeeMapper.save(admin);//当我保存  那么员工的id就有了

        shop.setAdminId(admin.getId());//将员工的id设置为管理员
        shopMapper.save(shop);   //当我保存  那么店铺的id就有了

        admin.setShopId(shop.getId());//将店铺的id设置给员工
        employeeMapper.update(admin);//将数据保存进去、
    }

}
